//
//  CinemaTableViewController.m
//  豆瓣3
//
//  Created by you on 15/10/23.
//  Copyright © 2015年 yym. All rights reserved.
//

#import "CinemaTableViewController.h"
#import "CinemaTableViewCell.h"
#import "CinemaModel.h"
#import "CallButton.h"
@interface CinemaTableViewController ()<CinemaTableViewDelegate>

@property (nonatomic,strong)NSMutableArray * arr;

@end

@implementation CinemaTableViewController

- (NSMutableArray *)arr
{
    if (_arr == nil) {
        self.arr = [NSMutableArray array];
    }
    return _arr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"影院";
    
    // 注册
    [self.tableView registerClass:[CinemaTableViewCell class] forCellReuseIdentifier:@"cell_cinema"];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self p_setInternet];
    
}

- (void)p_setInternet
{
    NSURL * url = [NSURL URLWithString:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/cinemalist.php"];
    NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url];
    NSURLSession * session = [NSURLSession sharedSession];
    NSURLSessionDataTask * task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (data != nil) {
            dispatch_async(dispatch_get_main_queue(), ^{
                NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingAllowFragments) error:nil];
                NSDictionary *dic1 = dic[@"result"];
                self.arr = dic1[@"data"];
                [self.tableView reloadData];
            });
        }
    }];
    [task resume];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.arr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CinemaTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell_cinema" forIndexPath:indexPath];
    
    NSDictionary *dic = self.arr[indexPath.row];
    cell.nameLabel.text = dic[@"cinemaName"];
    cell.addressLabel.text = dic[@"address"];
    cell.phoneLabel.text = dic[@"telephone"];
    
    cell.delegate = self;
    cell.callButton.indexPath = indexPath;
    
    return cell;
}

- (void)callButton:(CallButton *)sender
{
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"确认" message:@"将要拨打电话" preferredStyle:(UIAlertControllerStyleAlert)];
    UIAlertAction * yes = [UIAlertAction actionWithTitle:@"确认" style:(UIAlertActionStyleDestructive) handler:^(UIAlertAction * _Nonnull action) {
        NSDictionary *dic = self.arr[sender.indexPath.row];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@",dic[@"telephone"]]]];
        
    }];
    UIAlertAction * no = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addAction:yes];
    [alert addAction:no];
    
    [self presentViewController:alert animated:YES completion:^{
        
    }];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [CinemaTableViewCell cellHeight];
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
